package com.doubao.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.doubao.common.result.Result;
import com.doubao.user.dto.request.UserFeedbackRequest;
import com.doubao.user.dto.response.UserInfoResponse;
import com.doubao.user.entity.UserFeedback;
import com.doubao.user.service.UserFeedbackService;
import com.doubao.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户反馈控制器
 */
@RestController
@RequestMapping("/feedback")
@RequiredArgsConstructor
@Tag(name = "用户反馈管理", description = "用户反馈相关接口")
public class UserFeedbackController {

    private final UserService userService;
    private final UserFeedbackService userFeedbackService;

    /**
     * 提交反馈
     */
    @PostMapping("/submit")
    @Operation(summary = "提交反馈", description = "提交用户反馈信息")
    public Result<Void> submitFeedback(@Valid @RequestBody UserFeedbackRequest request) {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        return userFeedbackService.submitFeedback(userInfo.getId(), request);
    }

    /**
     * 获取用户反馈列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户反馈列表", description = "分页获取当前登录用户的反馈列表")
    public Result<IPage<UserFeedback>> getUserFeedbacks(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        return Result.success(userFeedbackService.getUserFeedbacks(userInfo.getId(), page, size));
    }

    /**
     * 获取反馈详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取反馈详情", description = "获取指定反馈的详细信息")
    public Result<UserFeedback> getFeedbackDetail(@PathVariable("id") Long feedbackId) {
        UserFeedback feedback = userFeedbackService.getFeedbackById(feedbackId);
        if (feedback == null) {
            return Result.failed("反馈不存在");
        }

        // 校验是否是当前用户的反馈
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        if (!feedback.getUserId().equals(userInfo.getId())) {
            return Result.failed("无权查看该反馈");
        }

        return Result.success(feedback);
    }

    /**
     * 删除反馈
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除反馈", description = "删除指定的反馈")
    public Result<Void> deleteFeedback(@PathVariable("id") Long feedbackId) {
        UserFeedback feedback = userFeedbackService.getFeedbackById(feedbackId);
        if (feedback == null) {
            return Result.failed("反馈不存在");
        }

        // 校验是否是当前用户的反馈
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        if (!feedback.getUserId().equals(userInfo.getId())) {
            return Result.failed("无权删除该反馈");
        }

        return userFeedbackService.deleteFeedback(feedbackId);
    }
}